document.onreadystatechange = function()
{
	if (document.readyState == "complete")
	{
		System.Gadget.settingsUI = "settings.html";
		System.Gadget.onSettingsClosed = settingsClosed;
	}
}


function settingsClosed(event)
{	
	if (event.closeAction == event.Action.commit)
	{
		loadGadget();
	}
}


function loadGadget()
{
	var userID = System.Gadget.Settings.read("userid");

	if (userID == "")
	{
		$("#firstView").fadeIn();
		$("#infoblock").fadeOut();
		return;
	}
	else
	{
		$("#firstView").fadeOut();
		$("#infoblock").fadeIn();
		
		var req = new XMLHttpRequest();
		req.open("GET", getUserProfile(), false);
		req.send();
		if (req.status === 200)
		{
			xmlParser(req.responseText);
		}
		setTimeout(loadGadget, 300000);	//reload the info every 5 mins
	}
}


function getUserProfile()
{
	var userid = System.Gadget.Settings.read("userid");
	var profileurl = "http://www.dreamincode.net/forums/xml.php?showuser=" + userid + "&uniq=" + new Date().getTime();

	return profileurl;
}


function xmlParser(xml)
{
	var det;
	det = new ActiveXObject("Msxml2.DOMDocument.3.0");
	det.loadXML(xml);
	
	propic.src = det.selectSingleNode("//ipb/profile/photo").text
	uname.innerText = det.selectSingleNode("//ipb/profile/name").text;
    reputation.innerText = det.selectSingleNode("//ipb/profile/reputation").text;
    views.innerText = det.selectSingleNode("//ipb/profile/views").text;
}
